//
//  AppDelegate.swift
//  张伟的微博
//
//  Created by 伟 on 16/2/26.
//  Copyright © 2016年 伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        let account = ZWUserAccountViewModel.sharedInstance.userAccount
        print("我取到了模型\(account)")
        //1.创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //2.设置跟控制器
        
        window?.rootViewController = rootViewControllerIsWho()
        setupAppearance()
        //3.可见
        window?.makeKeyAndVisible()
       
        
        
        return true
    }
    class func outSwitchViewController(isMain:Bool) {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(isMain)
    }
    //根控制器转换
    private func switchViewController(isMain:Bool) {
        let controller = isMain ? ZWMainViewController(): ZWWelcomeViewController()
        window?.rootViewController = controller
    }
    func setupAppearance() {
        let item = UIBarButtonItem.appearance()
        item.tintColor = UIColor.orangeColor()
        
    }
    
    //判断是否是新版本
    private func isNewVersion ()->Bool {
        //拿到当前版本
       let version =  NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //获取应用之前的版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxVersionKey)
        let newVersion = version != sandboxVersion
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(version, forKey: sandboxVersionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            print("当前微信版本")
            return true
            
        } else {
            print("jiu")
        return false
            
        }
    }
    
    //判断用户是否登录，返回相应控制器
    private func rootViewControllerIsWho () -> UIViewController{
       let userLodin =  ZWUserAccountViewModel.sharedInstance.userLodin
        if userLodin == false {
            return ZWMainViewController()
        }
        
        return isNewVersion() ? ZWNewFeatureViewControlle():ZWWelcomeViewController()
    }
  
   
  

}

